package com.bdsy.api.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bdsy.api.annotation.IgnoreAuth;
import com.bdsy.api.annotation.LoginUser;
import com.bdsy.api.entity.Token;
import com.bdsy.api.service.TokenService;
import com.bdsy.common.utils.Result;
import com.bdsy.common.utils.StringUtil;
import com.bdsy.system.model.User;
import com.bdsy.system.service.UserService;
import com.bdsy.system.shiro.Encrypt;
import com.bdsy.system.shiro.ShiroUtils;

@RestController
@RequestMapping(value = "/api")
public class ApiLoginController {

	@Autowired
	private TokenService tokenService;
	
	@Autowired 
	private UserService userService;
	
	
	@RequestMapping("/login")
	public Object login(String loginName,String loginPassword){
		
		User user = userService.getUsername(loginName);
		if(user == null){
			return Result.error("手机号或密码错误");
		}
		if(!user.getLoginPassword().equals(Encrypt.md5(loginPassword, loginName))){
			return Result.error("手机号或密码错误");
		}
			
		Map<String, Object> map = tokenService.createToken(user.getUserId());
	
		//暂时处理
		map.put("user", user);
		
		return Result.ok(map);
	}
	
	@IgnoreAuth
	@GetMapping("getUser")
	public Object getUser(String token,@LoginUser User user){
		System.out.println(token);
		Token userToken = tokenService.getToken(token);
		return user;
	}
	
}
